<template>
  <div class="resource">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <div v-if="openFlag" style="display:inline-block;margin-left: 20px;">
          <el-button
            @click="search"
            icon="el-icon-search"
            size="small"
            type="cyan"
            >搜索</el-button
          >
          <el-button
            icon="el-icon-folder-opened"
            type="primary"
            size="small"
            @click="openSearch"
            >展开</el-button
          >
          <el-button
            @click="cancelData"
            icon="el-icon-circle-close"
            size="small"
            >重置</el-button
          >
        </div>
        <div v-if="!openFlag" style="display:inline-block;margin-left: 20px;">
          <el-button
            @click="search"
            icon="el-icon-search"
            type="cyan"
            size="small"
            >搜索</el-button
          >
          <el-button
            @click="openSearch"
            icon="el-icon-folder-opened"
            type="primary"
            size="small"
            >收起</el-button
          >
          <el-button
            @click="cancelData"
            icon="el-icon-circle-close"
            size="small"
            >重置</el-button
          >
        </div>
      </div>
      <el-row :gutter="20">
        <el-form
          ref="params"
          :model="params"
          label-width="100px"
          size="small"
          class="searchForm"
        >
          <el-col :span="6">
            <el-form-item label="机构名称:" prop="companyName">
              <el-input
                v-model="params.authorityName"
                size="small"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人:" prop="linkman">
              <el-input
                v-model="params.linkman"
                size="small"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话:" prop="phone">
              <el-input
                v-model="params.phone"
                size="small"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="合作状态:" prop="status">
              <el-select
                v-model="params.status"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in examineList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间:" v-if="!openFlag" prop="createTime">
              <el-date-picker
                v-model="params.createTime"
                type="datetime"
                placeholder="选择日期时间"
                @change="createTime"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
    </el-card>

    <div class="container">
      <div class="handle-box">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="mr10"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </div>

      <!-- <el-card shadow="hover">
        <h4 style="margin-bottom: 10px">咨询公司管理</h4> -->
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="60"
          align="center"
        ></el-table-column>
        <el-table-column label="机构名称" min-width="260" align="center">
          <template slot-scope="scope">
            <span
              class="blue"
              style="text-decoration: underline; margin-left: 5px"
              @click="handleEdit(scope.$index, scope.row)"
              >{{ scope.row.authorityName }}</span
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="linkman"
          label="联系人"
          align="center"
          min-width="150"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="联系方式"
          align="center"
          min-width="150"
        ></el-table-column>
        <el-table-column label="合作状态" align="center" min-width="150">
          <template slot-scope="scope">
            <span>{{ status(scope.row.status) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" min-width="160">
          <template slot-scope="scope">{{
            formatT(scope.row.createTime)
          }}</template>
        </el-table-column>
        <el-table-column
          label="操作"
          min-width="230"
          align="center"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="small"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- </el-card> -->
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="600px">
      <el-form
        :rules="rules2"
        ref="formData"
        :model="formData"
        label-width="120px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="18">
            <el-form-item label="机构名称" prop="authorityName">
              <el-input
                v-model="formData.authorityName"
                :disabled="editFlags"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="联系人" prop="linkman">
              <el-input v-model="formData.linkman"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label="合作状态" prop="status">
              <el-select
                v-model="formData.status"
                filterable
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in examineList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="认证范围">
              <el-input
                :rows="6"
                type="textarea"
                v-model="formData.scope"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取消</el-button>
        <el-button @click="submitForm()" type="primary" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  company_add,
  companyList,
  company_update,
  company_delete,
  company_sure,
  company_get
} from "@/api/jigou_company/jigou_company";
import { getToken } from "@/utils/auth";
export default {
  components: {},
  props: [],
  data() {
    return {
      // 非多个禁用
      multiple: true,
      //删除
      ids: [],
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      examineList: [
        {
          id: "0",
          title: "待合作"
        },
        {
          id: "1",
          title: "已合作"
        }
      ],
      orderNum_show: false,
      examine: false,
      title666: "",
      rules2: {
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        authorityName: [
          { required: true, message: "请输入机构名称", trigger: "blur" }
        ],
        linkman: [
          { required: true, message: "请输入联系人名称", trigger: "blur" }
        ],
        status: [{ required: true, message: "请选择合作状态", trigger: "blur" }]
      },
      input: "",
      openFlag: true,
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10
      },
      pageTotal: 0,
      dialogVisible: false, // 是否展示编辑页面
      // 新增/编辑数据
      formData: {
        businessLicense: ""
      },
      customer: {
        customerId: ""
      },
      paramsFlag: 0,
      editFlags: false
    };
  },
  computed: {},
  created() {
    this.getData00();
  },
  mounted() {},
  watch: {},
  methods: {
    company_get(id) {
      company_get(id).then(res => {
        console.log(res);
        this.formData = res.data;
      });
    },
    // 键值配对
    status(val) {
      var str = this.examineList.find(function(item) {
        return item.id == val;
      });
      if (str) return str.title;
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        this.formData.businessLicense = res.url;
        console.log(this.formData.businessLicense);
        this.$message.success(`上传成功`);
      }
    },
    //   获取创建时间
    createTime(e) {
      console.log(e);
    },
    // 新增修改的方法
    submitForm() {
      this.$refs["formData"].validate(valid => {
        if (valid) {
          if (this.title666 == "新增") {
            company_add(this.formData).then(res => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(`新增成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          } else {
            company_update(this.formData).then(res => {
              if (res.code == 200) {
                this.$message.success(`修改成功`);
                this.dialogVisible = false;
                this.uploading = false;
                this.getData00();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开搜索
    openSearch() {
      this.openFlag = !this.openFlag;
    },
    // 重置搜索框
    cancelData() {
      this.resetForm("params");
      this.getData00();
    },
    handleAdd() {
      this.formData = {
        businessLicense: ""
      };
      this.dialogVisible = true;
      this.orderNum_show = false;
      this.examine = false;
      this.title666 = "新增";
    },
    handleEdit(index, row) {
      // this.formData = row;
      this.company_get(row.id);
      this.dialogVisible = true;
      this.orderNum_show = true;
      this.examine = false;
      this.title666 = "编辑";
    },
    handleExamine(index, row) {
      // this.formData = row;
      this.company_get(row.id);
      this.dialogVisible = true;
      this.orderNum_show = true;
      this.examine = true;
      this.title666 = "审核";
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map(item => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return company_delete(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    //获取数据
    getData00() {
      companyList(this.params).then(res => {
        if (res.code == "200") {
          this.tableData = res.rows;
          this.pageTotal = res.total;
          this.loading = false;
        }
      });
    },
    // 触发搜索按钮
    search() {
      console.log(this.params);
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    }
  }
};
</script>

<style scoped>
.resource {
  padding: 30px;
}
.container {
  margin-top: 20px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.compangImg {
  display: block;
  width: 280px;
  height: 200px;
}
</style>
